import Vue from 'vue'
import Vuex from 'vuex'
import '../main'

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        isAdmin: false,
        isLogin: false,
        good: {
            goodid: "",
            userid: "",
            username: "",
            name: "",
            price: "",
            num: "",
            img: "",
            introduction: "",
        },
        request: {
            requestid: "",
            userid: "",
            username: "",
            name: "",
            price: "",
            num: "",
            img: "",
            introduction: "",
        },
        search: "",
        talktoList: []
    },
    // {
    //     username: "nihac",
    //     userid: "1",
    //     userimg:
    //         "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",

    // },
    // {
    //     username: "huih",
    //     userid: "456",
    //     userimg:
    //         "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",

    // },
    mutations: {
        admin(state) {
            state.isAdmin = true
        },
        login(state) {
            state.isLogin = true;
            var user = {
                username: sessionStorage.getItem("username"),
                userid: sessionStorage.getItem("userid"),
                userimg: sessionStorage.getItem("userimg"),
                email: sessionStorage.getItem("email")
            }
            localStorage.setItem("LoginState", JSON.stringify(user));
        },
        logout(state) {
            state.isLogin = false;
            localStorage.removeItem("LoginState")
        },
        refresh() {
            var user = {
                username: sessionStorage.getItem("username"),
                userid: sessionStorage.getItem("userid"),
                userimg: sessionStorage.getItem("userimg"),
                email: sessionStorage.getItem("email")
            }
            localStorage.setItem("LoginState", JSON.stringify(user));
        },
        setGood(state, good) {
            state.good.goodid = good.goodid;
            state.good.userid = good.userid;
            state.good.username = good.usernaem;
            state.good.name = good.name;
            state.good.price = good.price;
            state.good.num = good.num
            state.good.img = good.img;
            state.good.introduction = good.introduction;
        },
        setRequest(state, request) {
            state.request.introduction = request.requestid;
            state.request.userid = request.userid;
            state.request.username = request.username;
            state.request.name = request.name;
            state.request.price = request.price;
            state.request.num = request.num;
            state.request.img = request.img;
            state.request.introduction = request.introduction;
        },
        setSearch(state, search) {
            state.search = search
        },
        initTalktoList(state) {
            state.talktoList = JSON.parse(localStorage.getItem(sessionStorage.getItem("userid")));
        },
        initLog(state) {
            var s = localStorage.getItem("LoginState");
            if (s !== null) {
                var user = JSON.parse(s)
                sessionStorage.setItem("username", user.username);
                sessionStorage.setItem("userid", user.userid);
                sessionStorage.setItem("userimg", user.userimg);
                sessionStorage.setItem("email", user.email);
                state.isLogin = true;
            }
        },
        talkto(state, user) {
            var u = { username: user.username, userid: user.userid, userimg: user.userimg };
            if (state.talktoList.length == 0) {
                state.talktoList.splice(0, 0, u);
                var list = JSON.stringify(state.talktoList);
                localStorage.setItem(sessionStorage.getItem("userid"), list);
                return;
            }
            var index = state.talktoList.findIndex((item) => item.userid === u.userid)
            if (index >= 0) {
                if (index !== 0) {
                    state.talktoList.unshift(state.talktoList.splice(index, 1)[0]);
                }
            } else {
                state.talktoList.splice(0, 0, u)
            }
            var list = JSON.stringify(state.talktoList);
            localStorage.setItem(sessionStorage.getItem("userid"), list);
        },
        popTalk(state, user) {
            var index = state.talktoList.findIndex((item) => item.userid === user.userid)
            state.talktoList.splice(index, 1);
            var list = JSON.stringify(state.talktoList);
            localStorage.setItem(sessionStorage.getItem("userid"), list);
            var msg = JSON.parse(localStorage.getItem("msg" + sessionStorage.getItem("userid")));
            if (msg == null) return
            for (var i = 0; i < msg.length; i++) {
                if (msg[i].fromid == user.userid || msg[i].toid == user.userid) {
                    console.log(i);
                    msg.splice(i, 1);
                    i--;
                }
            }
        }
    }
})

export default store